package de.slothsoft.wasbinich.swingclient.common;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RadialGradientPaint;
import java.awt.Rectangle;
import java.awt.geom.Point2D;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;

public class RoundButton extends JButton {

    private static final long serialVersionUID = 7419865506929826575L;

    private int arc = 10;
    private int borderWidth = 2;
    private Color borderColor;
    private Color firstColor;
    private Color secondColor;

    public RoundButton() {
	super();
	init();
    }

    public RoundButton(Action a) {
	super(a);
	init();
    }

    public RoundButton(String text) {
	super(text);
	init();
    }

    public RoundButton(Icon icon) {
	super(icon);
	init();
    }

    private void init() {
	setContentAreaFilled(false);
	setBorderPainted(false);
	setOpaque(false);
	setFocusPainted(false);
    }

    @Override
    public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D) g;
	Rectangle rect = g2.getClipBounds();

	int halfBorderWidth = this.borderWidth / 2;

	if (this.firstColor != null && this.secondColor != null) {
	    RadialGradientPaint paint = new RadialGradientPaint(
		    new Point2D.Double(rect.width / 2.0, rect.height / 2.0), rect.width / 2.0f, new float[] { 0.0f,
			    1.0f }, new Color[] { this.firstColor, this.secondColor });
	    g2.setPaint(paint);
	    g2.fillRoundRect(halfBorderWidth, halfBorderWidth, rect.width - this.borderWidth - 1, rect.height
		    - this.borderWidth - 1, this.arc, this.arc);
	}
	if (this.borderColor != null) {
	    g2.setPaint(null);
	    g2.setStroke(new BasicStroke(this.borderWidth));
	    g2.setColor(this.borderColor);
	    g2.drawRoundRect(halfBorderWidth, halfBorderWidth, rect.width - this.borderWidth - 1, rect.height
		    - this.borderWidth - 1, this.arc, this.arc);

	    g2.setStroke(new BasicStroke());
	}
	super.paint(g);
    }

    public Color getBorderColor() {
	return this.borderColor;
    }

    public void setBorderColor(Color borderColor) {
	this.borderColor = borderColor;
    }

    public Color getFirstColor() {
	return this.firstColor;
    }

    public void setFirstColor(Color firstColor) {
	this.firstColor = firstColor;
    }

    public Color getSecondColor() {
	return this.secondColor;
    }

    public void setSecondColor(Color secondColor) {
	this.secondColor = secondColor;
    }

    public void setArc(int arc) {
	this.arc = arc;
    }

    public int getArc() {
	return this.arc;
    }

    public void setBorderWidth(int borderWidth) {
	this.borderWidth = borderWidth;
    }

    public int getBorderWidth() {
	return this.borderWidth;
    }

}
